﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Enumerable
{


    public class Player
    {
        public int m_ID;

        public double _X = 0;
        public double _Y = 0;



        public List<ProcessInfo> ProcessList = new List<ProcessInfo>();
        public List<int> _DestroyProcessList = new List<int>();

        public Player(int id)
        {
            m_ID = id;
        }

        public void AddProcess(IEnumerator pProcess, int id)
        {
            ProcessInfo pP = new ProcessInfo(id);
            pP.m_ProcessEnum = pProcess;

            ProcessList.Add(pP);

        }


        public void ClearProcess(int id)
        {
            _DestroyProcessList.Add(id);


        }

        public void Update()
        {

            // 
            for (int index = ProcessList.Count - 1; index >= 0; index--)
            {
                ProcessInfo pProcess = ProcessList[index];

                if (pProcess.m_Condition != null)
                    if (pProcess.m_Condition.Do() == false)
                    {// 선행작업이 완료되지 않았다.
                        continue;
                    }

                if (pProcess.m_ProcessEnum.MoveNext() == true)
                {

                    pProcess.m_Condition = (ICondition)pProcess.m_ProcessEnum.Current;
                    continue;

                }
                else
                {
                    ProcessList.Remove(pProcess);
                }
            }

            // 요청된 프로세스를 죽인다.

            foreach (int id in _DestroyProcessList)
            {
                foreach (ProcessInfo pInfo in ProcessList)
                {
                    if (pInfo._id == id)
                    {
                        pInfo.m_Condition.Cancel();
                        ProcessList.Remove(pInfo);
                        break;
                    }
                }

            }
        }

    }
}
